#include "CanonicalConvTester.h"
#include "CanonicalConv.h"
#include <algorithm>
#include <cctype> 
#include <iostream>

using namespace std;

bool testCase(const string baseUrl,const string expected)
{
		CanonicalConv conv;
		string testUrl = baseUrl;
		string result = conv.toCanonical(testUrl);
		if(result.compare(expected) == 0)
		{
			cout << "OK on baseURL + " << baseUrl << endl;
			return true;
		}
		else
		{
			cout << "NOK on baseURL + "<< baseUrl << endl;
			cout << "#### Retornou + " << result  <<endl;
			return false;
		}
}

CanonicalConvTester::CanonicalConvTester()
{
}

CanonicalConvTester::~CanonicalConvTester()
{
}

void CanonicalConvTester::runTests()
{
	
	//TODO: Think on more testcases!
	
	string baseUrl = "http://www.uol.com.br";
	string newUrl = baseUrl;
	string extraUrl ="";
	
	/* Testing for the baseURL */
	newUrl.append("");
	testCase(newUrl,baseUrl);

	
	/* Testing for baseUrl with ending /" */
	newUrl = baseUrl;
	newUrl.append("/");
	testCase(newUrl,baseUrl);
	
	/* Testing for baseUrl with index.html \" */
	newUrl = baseUrl;
	newUrl.append("/index.html");
	testCase(newUrl,baseUrl);
	
	/* Testing for baseUrl with index.htm \" */
	newUrl = baseUrl;
	newUrl.append("/index.htm");
	testCase(newUrl,baseUrl);
	
	/* Testing for baseUrl with default.aspx \" */
	newUrl = baseUrl;
	newUrl.append("/default.aspx");
	testCase(newUrl,baseUrl);
	
	/* Testing for baseUrl with default.asp \" */
	newUrl = baseUrl;
	newUrl.append("/default.asp");
	testCase(newUrl,baseUrl);
	
	/* Testing for baseUrl with index.html/ \" */
	newUrl = baseUrl;
	newUrl.append("/index.html/");
	testCase(newUrl,baseUrl);
	
	/* Testing for baseUrl with index.htm/ \" */
	newUrl = baseUrl;
	newUrl.append("/index.htm/");
	testCase(newUrl,baseUrl);
	
	/* Testing for baseUrl with default.aspx/ \" */
	newUrl = baseUrl;
	newUrl.append("/default.aspx/");
	testCase(newUrl,baseUrl);
	
	/* Testing for baseUrl with default.asp/ \" */
	newUrl = baseUrl;
	newUrl.append("/default.asp/");
	testCase(newUrl,baseUrl);
	
	/* Testing for baseUrl with :80 as a port \" */
	newUrl = baseUrl;
	newUrl.append(":80");
	testCase(newUrl,baseUrl);

	/* Testing for baseUrl with :80 as a port and extra dirs \" */
	newUrl = baseUrl;
	newUrl.append(":80/arthur");
	extraUrl = baseUrl;
	extraUrl.append("/arthur");
	testCase(newUrl,extraUrl);
	
	/*Testing for removal of # */
	newUrl = baseUrl;
	extraUrl = baseUrl;
	extraUrl.append("/a.html");
	newUrl.append("/a.html#dhflagsdflasgskagfask");
	testCase(newUrl,extraUrl);
	
	/* Testing for lowecapitalization */
	newUrl = baseUrl;
	transform( newUrl.begin(), newUrl.end(), newUrl.begin(),(int(*)(int)) toupper); 
	testCase(newUrl,baseUrl);
	
	/* Testing for (..) relative operator */
	newUrl = baseUrl;
	newUrl.append("/teste/a/b/../../c/index.htm");
	extraUrl = baseUrl;
	extraUrl.append("/teste/c");
	testCase(newUrl,extraUrl);
	
	/* Testing for (.) relative operator */
	newUrl = baseUrl;
	newUrl.append("/teste/a/./c/../index.htm");
	extraUrl = baseUrl;
	extraUrl.append("/teste/a");
	testCase(newUrl,extraUrl);
	
	/* Testing for (%) escape operator */
	newUrl = baseUrl;
	newUrl.append("/teste/a/./c/../la.htm%2sd%blah");
	extraUrl = baseUrl;
	extraUrl.append("/teste/a/la.htm%2SD%blah");
	testCase(newUrl,extraUrl);
	
	
}



